package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class MatchPage extends Utils {
    private static String url = "http://47.108.48.236:8081/game_hall.html";
    public MatchPage() {
        super(url);
    }

    /**
     * 未登录状态下进入游戏大厅
     */
    public void noLoginToMatch() {
        getScreenShot(getClass().getName());
        // 通过页面标题检查是否跳转到登录页面
        String expect = webDriver.getTitle();
        assert expect.equals("登录");
    }

    public void match() {
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        // 1. 进行登录
        webDriver.findElement(By.cssSelector("#name")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 2. 等待页面加载
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#match-button")));
        // 3. 开始匹配
        webDriver.findElement(By.cssSelector("#match-button")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#match-button"), "匹配中...(点击停止)"));
        WebElement element = webDriver.findElement(By.cssSelector("#match-button"));
        String content = element.getText();
        assert content.equals("匹配中...(点击停止)");

        // 4. 停止匹配
        webDriver.findElement(By.cssSelector("#match-button")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#match-button"), "开始匹配"));
        element = webDriver.findElement(By.cssSelector("#match-button"));
        content = element.getText();
        assert content.equals("开始匹配");
    }
}
